package com.idealighter.game.games.shuihu.struct;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.ShuihuRoomDomain;
import com.idealighter.game.games.common.AbstractRoom;
import com.idealighter.game.games.common.AbstractSeat;
import com.idealighter.game.games.common.InstanceState;
import com.idealighter.game.games.shuihu.manager.ShuiHuDataMgr;
import com.idealighter.game.server.context.ApplicationContext;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 水浒传房间 .
 * 
 * @date 2015年9月28日 下午2:17:11
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ShuiHuRoom extends AbstractRoom {

  private final int instanceId;

  private InstanceState instanceState;

  private ShuihuRoomDomain roomDomain;

  // 桌子
  private final Map<Integer, ShuiHuTable> tables;

  /**
   * 构造函数.
   * 
   * @param instanceId 实例id.
   * @param roomDomain .
   */
  public ShuiHuRoom(int instanceId, ShuihuRoomDomain roomDomain) {
    super(roomDomain.getId(), roomDomain.getName());
    this.instanceState = InstanceState.NORMAL;
    this.instanceId = instanceId;
    this.roomDomain = roomDomain;
    this.tables = createTables(roomDomain.getTableNum());
  }

  /**
   * 创建桌子.
   * 
   * @param num 桌子个数.
   * @return 桌子映射map.
   */
  private Map<Integer, ShuiHuTable> createTables(int num) {
    Map<Integer, ShuiHuTable> tables = new LinkedHashMap<>();
    for (int i = 0; i < num; i++) {
      int tableId = instanceId * ShuiHuTable.MAX_TABLE + i + 1;
      tables.put(tableId, new ShuiHuTable(tableId, this));
    }

    return tables;
  }

  public ShuiHuTable removeTable(int tableId) {
    return tables.remove(tableId);
  }

  @Override
  public Game game() {
    return Game.SHUI_HU_ZHUAN;
  }

  @Override
  public AbstractSeat seat(long playerId) {
    return ApplicationContext.getBean(ShuiHuDataMgr.class).getPlayerSeat(playerId);
  }

  @Override
  public Collection<ShuiHuTable> tables() {

    return tables.values();
  }

}
